package com.css.zfzx.fwsjcj.modules.structure.controller;

import com.alibaba.fastjson.JSONObject;
import com.css.bpm.platform.base.response.RestResponse;
import com.css.bpm.platform.utils.PlatformPageUtils;
import com.css.zfzx.fwsjcj.commen.util.DownloadUtils;
import com.css.zfzx.fwsjcj.modules.structure.entity.FwMulConcreteStructureEntity;
import com.css.zfzx.fwsjcj.modules.structure.service.FwMulConcreteStructureService;
import com.css.zfzx.fwsjcj.modules.structure.vo.StructureQueryVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Arrays;
import java.util.List;

/**
 * @author leon
 * @Title: FwMulConcreteStructureController
 * @Description: 多层钢筋混凝土
 * @date 2020/6/4 14:15
 */
@RestController
@RequestMapping("/fw/mulConcrete")
@Slf4j
public class FwMulConcreteStructureController {
    @Autowired
    private FwMulConcreteStructureService mulConcreteStructureService;
    @Autowired
    DownloadUtils downloadUtils;

    /**
     * 增加
     *
     * @param fwMulConcreteStructureEntity
     * @return
     */
    @PostMapping("save")
    public RestResponse save(@RequestBody FwMulConcreteStructureEntity fwMulConcreteStructureEntity) {
        RestResponse restResponse = null;
        JSONObject jsonObject = new JSONObject();
        try {
            mulConcreteStructureService.save(fwMulConcreteStructureEntity);
            jsonObject.put("message", "保存成功");
            restResponse = RestResponse.succeed(jsonObject);
        } catch (Exception e) {
            String errormessage = "保存失败";
            log.error(errormessage, e);
            restResponse = RestResponse.fail(errormessage);
        }
        return restResponse;
    }

    /**
     * 修改
     *
     * @param fwMulConcreteStructureEntity
     * @return
     */
    @PostMapping("update")
    public RestResponse update(@RequestBody FwMulConcreteStructureEntity fwMulConcreteStructureEntity) {
        RestResponse restResponse = null;
        JSONObject jsonObject = new JSONObject();
        try {
            mulConcreteStructureService.update(fwMulConcreteStructureEntity);
            jsonObject.put("message", "修改成功");
            restResponse = RestResponse.succeed(jsonObject);
        } catch (Exception e) {
            String errormessage = "修改失败";
            log.error(errormessage, e);
            restResponse = RestResponse.fail(errormessage);
        }
        return restResponse;
    }

    /**
     * 分页
     *
     * @param request
     * @param structureQueryVo
     * @return
     */
    @GetMapping("page")
    public RestResponse getPage(HttpServletRequest request, StructureQueryVo structureQueryVo) {
        RestResponse restResponse = null;
        try {
            int curPage = Integer.valueOf(request.getParameter(PlatformPageUtils.CUR_PAGE));
            int pageSize = Integer.valueOf(request.getParameter(PlatformPageUtils.PAGE_SIZE));
            JSONObject jsonObject = mulConcreteStructureService.getPage(structureQueryVo, curPage, pageSize);
            restResponse = RestResponse.succeed(jsonObject);
        } catch (Exception e) {
            String errormessage = "查询失败";
            log.error(errormessage, e);
            restResponse = RestResponse.fail(errormessage);
        }
        return restResponse;
    }

    /**
     * 根据ID获取信息
     *
     * @param id
     * @return
     */
    @GetMapping("getone/{id}")
    public RestResponse getOne(@PathVariable String id) {
        RestResponse restResponse = null;
        try {
            FwMulConcreteStructureEntity mulConcreteStructureEntity = mulConcreteStructureService.findById(id);
            restResponse = RestResponse.succeed(mulConcreteStructureEntity);
        } catch (Exception e) {
            String errormessage = "查询失败";
            log.error(errormessage, e);
            restResponse = RestResponse.fail(errormessage);
        }
        return restResponse;
    }

    /**
     * 获取所有信息
     *
     * @return
     */
    @GetMapping("getall")
    public RestResponse getAll() {
        RestResponse restResponse = null;
        try {
            List<FwMulConcreteStructureEntity> mulConcreteStructureEntitys = mulConcreteStructureService.findAll();
            restResponse = RestResponse.succeed(mulConcreteStructureEntitys);
        } catch (Exception e) {
            String errormessage = "查询失败";
            log.error(errormessage, e);
            restResponse = RestResponse.fail(errormessage);
        }
        return restResponse;
    }

    /**
     * 逻辑删除
     *
     * @param id
     * @return
     */
    @PostMapping("/del/{id}")
    public RestResponse delete(@PathVariable String id) {
        RestResponse restResponse = null;
        JSONObject jsonObject = new JSONObject();
        try {
            mulConcreteStructureService.delete(id);
            jsonObject.put("message", "删除成功");
            restResponse = RestResponse.succeed(jsonObject);
        } catch (Exception e) {
            String errormessage = "删除失败";
            log.error(errormessage, e);
            restResponse = RestResponse.fail(errormessage);
        }
        return restResponse;
    }

    @PostMapping("/submit")
    public RestResponse submit(@RequestBody String ids) {
        RestResponse restResponse = null;
        JSONObject jsonObject = new JSONObject();
        try {
            List<String> idList = Arrays.asList(ids.split(","));
            mulConcreteStructureService.submit(idList);
            jsonObject.put("message", "提交成功");
            restResponse = RestResponse.succeed(jsonObject);
        } catch (Exception e) {
            String errormessage = "提交失败";
            log.error(errormessage, e);
            restResponse = RestResponse.fail(errormessage);
        }
        return restResponse;
    }

    /**
     * 下载多层钢筋混凝土模板
     *
     * @return
     */
    @GetMapping("downMould")
    public RestResponse downMould() {
        RestResponse restResponse = null;
        JSONObject jsonObject = new JSONObject();
        try {
            String excel = "多层钢筋混凝土结构.xlsx";
            downloadUtils.download(excel);
            jsonObject.put("message", "下载成功");
            restResponse = RestResponse.succeed(jsonObject);
        } catch (Exception e) {
            String errormessage = "下载失败";
            log.error(errormessage, e);
            restResponse = RestResponse.fail(errormessage);
        }
        return restResponse;
    }

    /**
     * 导入
     *
     * @param taskName
     * @param taskId
     * @param file
     * @param response
     * @return
     */
    @PostMapping("export/{taskName}/{taskId}")
    public RestResponse export(@PathVariable("taskName") String taskName, @PathVariable("taskId") String taskId, @RequestParam("file") MultipartFile file, HttpServletResponse response) {
        RestResponse restResponse = null;
        try {
            String s = mulConcreteStructureService.exportExcel(taskName, taskId, file, response);
            restResponse = RestResponse.succeed(s);
        } catch (Exception e) {
            String errormessage = "导入失败";
            log.error(errormessage, e);
            restResponse = RestResponse.fail(errormessage);
        }
        return restResponse;
    }
}
